//
//  QDUtilities-Swift.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/2/22.
//  Copyright © 2019 YueWen. All rights reserved.
//

import Foundation

//MARK: Color

extension UIColor {
    
    /// 生成图片
    public var ritl_image : UIImage {
        
        let rect = CGRect(x: 0.0, y: 0.0, width: 1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        
        let context = UIGraphicsGetCurrentContext()
        context?.setFillColor(self.cgColor)
        context?.fill(rect)
        
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return image!
    }
}


extension Int{
    
    /// 生成颜色
    public var ritl_color: UIColor {
        
        if self < 0 { return .black }
        let color = self.colorBrightness
        return UIColor(red: color, green: color, blue: color, alpha: 1)
    }
    
    
    /// 颜色的色值，self/255.0
    public var colorBrightness: CGFloat {
        
        if self < 0 || self > 255 { return 0 }
        return CGFloat(self) / 255
    }
}



extension Int {
    /// 生成图片
    public var ritl_image : UIImage {
        return self.ritl_color.ritl_image
    }
}



//MARK: HUD
extension MBProgressHUD {
    
    static func showMBHud(text: String, on view:UIView?, delayHidden time:TimeInterval){
        guard let onView = view else {
            return
        }
        
        if let showHUD = MBProgressHUD(for: onView) {
            showHUD.label.text = text;
            DispatchQueue.main.async {
                showHUD.hide(animated: true, afterDelay: time)
            }
            return
        }
        
        let hud = MBProgressHUD.showAdded(to: onView, animated: true)
        hud.mode = .text
        hud.bezelView.color = UIColor.black.withAlphaComponent(0.5)
        hud.label.text = text
        hud.label.textColor = .white
        hud.label.font = Font.regular.font(size: 14)
        
        DispatchQueue.main.async {
            hud.hide(animated: true, afterDelay: time)
        }
    }
    
    
    static func showWaitHUD(on view: UIView) {
        
        if let hud = MBProgressHUD(for: view) {
            hud.hide(animated: false); return
        }
        
        let hud = MBProgressHUD.showAdded(to: view, animated: true)
        hud.bezelView.style = .blur
        hud.show(animated: true)
    }
}


extension UIView {
    
    /// show
    func showTextHUD(text: String,delayHidden time: TimeInterval = 1){
        MBProgressHUD.showMBHud(text: text, on: self, delayHidden: time)
    }
    
    /// wait
    func showWatingHUD() {
        MBProgressHUD.showWaitHUD(on: self)
    }
    
    /// dimiss
    func dismissHUD() {
        MBProgressHUD.hide(for: self, animated: true)
    }
}




//MARK: Font

/// 字体
enum Font: String {
    
    case regular = "PingFangSC-Regular"
    case medium = "PingFangSC-Medium"
    case bold = "PingFangSC-Bold"
    case light = "PingFangSC-Light"
    case semibold = "PingFangSC-Semibold"
}


extension Font {
    func font(size: CGFloat) -> UIFont {
        return UIFont.safe_font(name: self.rawValue, size: size)
    }
}


extension UIFont {
    
    /// 根据字体名字获取font对象，如果不存在，返回系统默认字体
    ///
    /// - Parameters:
    ///   - name: 字体类型
    ///   - size: 字体大小
    public class func safe_font(name: String, size: CGFloat) -> UIFont{
        if let font = UIFont(name: name, size: size) {
            return font
        }
        return UIFont.systemFont(ofSize: size)
    }
}


//MARK: 字符串

extension String {
    
    func limit(_ limit: Int = 7) -> String {
        if limit < 0 || self.count <= limit { return self }
        let idx = self.index(self.startIndex, offsetBy: limit - 1)
        return "\(String(self[..<idx]))..."
    }
}


extension String {
    
    /// 是否含有中文
    var containChinese: Bool {
        for (_, char) in enumerated() {
            if "\u{4E00}" <= char  && char <= "\u{9FA5}" {
                return true
            }
        }
        return false
    }
}


extension String {
    
    var namedImage: UIImage? {
//        let image = UIImage(named: self)
        return UIImage(named: self)
    }
}



//MARK: Byte转换成大小描述
extension UInt {
    
    ///
    var sizeString: String {
        
        let standard = 1024.0
        
        if Double(self) > standard * standard {//MB
            return String(format: "%.1fMB", Double(self) / standard / standard)
        }
        
        if Double(self) > standard {
            return String(format: "%.0fKB", Double(self) / standard)
        }
        
        return "\(self)B"
    }
}
